package interview;

import java.util.LinkedList;
import java.util.List;

public class interview_16_25 {
    class LRUCache {

        List<int[]> list;
        int size;
        int currentSize;
        public LRUCache(int capacity) {
            list=new LinkedList<>();
            size=capacity;
            currentSize=0;
        }

        public int get(int key) {
            int value=-1;
            for(int[] item:list){
                if(item[0]==key){
                    int[] tmp=item;
                    list.remove(item);
                    list.add(tmp);
                    value=tmp[1];
                    break;
                }
            }
            return value;
        }

        public void put(int key, int value) {
            boolean flag=false;
            for(int[] item:list){
                if(item[0]==key){
                    item[1]=value;
                    int[] tmp=item;
                    list.remove(item);
                    list.add(tmp);
                    flag=true;
                    break;
                }
            }
            if(!flag){
                if(size==currentSize){
                    if(size==0) return;
                    list.remove(0);
                    list.add(new int[]{key,value});

                }else{
                    list.add(new int[]{key,value});
                    currentSize++;
                }
            }
        }
    }
}
